package me.yuzho.study.concurrency;

import static net.mindview.util.Print.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Meal {
    private final int orderNum;
    public Meal(int orderNum) { this.orderNum = orderNum; }
    public String toString() { return "Meal " + orderNum; }
}

class WaitPerson implements Runnable {
    private Restaurant restaurant;
    
    public WaitPerson(Restaurant r) { restaurant = r; }

    @Override
    public void run() {
        
        try {
            while(!Thread.interrupted()) {
                synchronized (this) {
                    while(restaurant.meal == null) {
                        print("WaitPerson: wait for the chef to produce a maal");
                        wait(); // ... for the chef to produce a meal
                    }
                }
                
                print("Waitperson got " + restaurant.meal);
                
                synchronized(restaurant.chef) {
                    restaurant.meal = null;
                    restaurant.chef.notifyAll(); // Ready for another 
                }
                
            }
        } catch (InterruptedException e) {
            
            print("WaitPerson interrupted");
        }
        
    }
}

class Chef implements Runnable {
    private Restaurant restaurant;
    private int count = 0;
    public Chef(Restaurant r) {
        restaurant = r;
    }

    @Override
    public void run() {
        try {
            
            while(!Thread.interrupted()) {
                synchronized (this) {
                    while(restaurant.meal != null) { // 菜已经准备好时，将线程挂起，等待waitPerson的唤醒
                        print("Chef: wait for the meal to be taken");
                        wait(); // ... for the meal to be taken
                    } 
                }

                if(++count == 10) {
                    print("out of food, closing");
                    restaurant.exec.shutdownNow();
                }
                
                printnb("Order up! ");
                
                synchronized (restaurant.waitPerson) { //获取waitPerson的锁，叫停了Wait person对菜是否准备好的检查
                    restaurant.meal = new Meal(count); // 做菜
                    restaurant.waitPerson.notifyAll(); // 唤醒扶起的waitPerson线程
                }
            }
            
            TimeUnit.MILLISECONDS.sleep(100);
            
        } catch (InterruptedException e) {
            print("Chef interrupted");
        }
        
    }
}

/** 生产者与消费者
 * The producer-consumer approach to task cooperation
 * @author yzhou-citrix
 *
 */
public class Restaurant {

    Meal meal = null; // 菜
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPerson waitPerson = new WaitPerson(this); //服务员（消费者）
    Chef chef = new Chef(this); //厨师 (生产者)
    
    public Restaurant() { // 饭店中的厨师与服务员开始工作，厨师准备好菜时，通知服务员，服务员上菜，如此反复
        print("开工：");
        exec.execute(chef);
        exec.execute(waitPerson);
    }
    
    public static void main(String[] args) {
        new Restaurant();
    }
}
